package com.zhiwu.uhf.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.zhiwu.uhf.R;
import com.zhiwu.uhf.data.PublicData;
import com.zhiwu.uhf.entity.ComparedInfo;

import java.util.List;
import java.util.Map;

public class EpcListAdapter extends RecyclerView.Adapter<EpcListAdapter.ViewHolder>  {
    private List<ComparedInfo> beanList;
    private Context context;
    public EpcListAdapter(Context context, List<ComparedInfo> beanList) {
        this.beanList = beanList;
        this.context = context;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout itemOnClick;
        private TextView tvRouteName;
        private ImageView multiple_choice;


        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvRouteName = itemView.findViewById(R.id.tvRouteName);
            itemOnClick = itemView.findViewById(R.id.itemOnClick);
            multiple_choice = itemView.findViewById(R.id.multiple_choice);
        }
    }

    @NonNull
    @Override
    public EpcListAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new EpcListAdapter.ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.recycle_epc_adapter, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull EpcListAdapter.ViewHolder holder, int position) {
        holder.itemOnClick.setOnClickListener(v -> {
            if (onItemClickListener != null) {
                onItemClickListener.OnItemClick(v, beanList, position);
            }
        });
        ComparedInfo data = beanList.get(position);
        boolean isChecked = Boolean.TRUE.equals(data.getChecked());
        holder.multiple_choice.setOnClickListener(v -> {
            if (setOnItemmultiple != null) {
                setOnItemmultiple.OnItemClick(v, beanList, position);
                data.setChecked(!isChecked);
                holder.multiple_choice.setSelected(!isChecked);
            }
        });
        if(data.getRouteCode() == null) {
            holder.tvRouteName.setText( (position+1) + " 箱号: " + data.getContainer());
            holder.tvRouteName.setTextColor(Color.RED);
        } else {
            holder.tvRouteName.setText( (position+1) + " 箱号: " + data.getContainer() + " 线路: " + data.getRouteName());
            holder.tvRouteName.setTextColor(Color.BLACK);
        }




    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return beanList.size();
    }

    private int mPosition = -1;

    public int getmPosition() {
        return mPosition;
    }

    public void setmPosition(int mPosition) {
        this.mPosition = mPosition;
    }

    private EpcListAdapter.OnItemClickListener onItemClickListener;
    private EpcListAdapter.OnItemClickListener setOnItemmultiple;
    private EpcListAdapter.OnItemClickListener setOnItemshowDialog;

    public void setOnItemClickListener(EpcListAdapter.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setOnItemmultiple(EpcListAdapter.OnItemClickListener setOnItemmultiple) {
        this.setOnItemmultiple = setOnItemmultiple;
    }

    public void setOnItemshowDialog(EpcListAdapter.OnItemClickListener setOnItemshowDialog) {
        this.setOnItemshowDialog = setOnItemshowDialog;
    }

    public interface OnItemClickListener {
        void OnItemClick(View view, List<ComparedInfo> data, int position);
    }

}
